package semiProject_army.mvc.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import semiProject_army.mvc.beans.BoardBean;
import semiProject_army.mvc.beans.BoardDAO;

public class WriteAction implements ActionHandler {

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse res)
			throws IOException, ServletException {
		
		System.out.println("액션탔당헤헤");
		String returnPage = viewPage;
		req.setCharacterEncoding("UTF-8");
				
		if(req.getMethod().equalsIgnoreCase("get")){
			
			req.setAttribute("action", "freeboard/write");
			System.out.println("get이야?");
		}
		
		else if(req.getMethod().equalsIgnoreCase("post")) {
			System.out.println("post야?");
			BoardBean board = new BoardBean();
			
			board.setWriter(req.getParameter("writer"));
			board.setTitle(req.getParameter("title"));
			board.setContent(req.getParameter("content"));
			board.setPass(req.getParameter("pass"));
			/*board.setRegDate(now());*/

			System.out.println(req.getParameter("writer"));
			
			BoardDAO dao = BoardDAO.getInstance();
			if(dao.insertBoard(board)>0){
				returnPage = "r|list.mvc";
			} else {
				returnPage = "r|error.mvc?code=123";
			}
			
		}
		
		return returnPage;
	}
	
}